package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.push.ClientPushRecord;
import com.yanqu.road.entity.servercenter.ClientPushContent;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserPushMsgDaoImpl extends TempDataObjectDao<ClientPushRecord> {

    public static String getClientPushRecordSql = "select * from t_u_client_msg_push";

    public Map<Long, Map<Integer, ClientPushRecord>> getClientPushRecord() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, Map<Integer, ClientPushRecord>> clientPushRecordMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getClientPushRecordSql);
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                ClientPushRecord clientPushRecord = parse(rs);
                if (!clientPushRecordMap.containsKey(clientPushRecord.getUserId())) {
                    clientPushRecordMap.put(clientPushRecord.getUserId(), new ConcurrentHashMap<>());
                }

                clientPushRecordMap.get(clientPushRecord.getUserId()).put(clientPushRecord.getType(), clientPushRecord);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return clientPushRecordMap;
    }


    public static String updateClientPushRecordSql = "update t_u_client_msg_push set times=?,updateTime=? where userId=? and type=?";

    @Override
    protected boolean updateData(ClientPushRecord clientPushRecord) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, clientPushRecord.getTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, clientPushRecord.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, clientPushRecord.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, clientPushRecord.getType()));
        if (getDb().executeNoneQuery(updateClientPushRecordSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertClientPushRecordSql = "insert into t_u_client_msg_push(userId,type,times,updateTime) values (?,?,?,?)";

    @Override
    protected boolean addData(ClientPushRecord clientPushRecord) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, clientPushRecord.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, clientPushRecord.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, clientPushRecord.getTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, clientPushRecord.getUpdateTime()));
        if (getDb().executeNoneQuery(insertClientPushRecordSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ClientPushRecord parseData(ResultSet rs) throws SQLException {
        ClientPushRecord clientPushRecord = new ClientPushRecord();
        clientPushRecord.setUserId(rs.getLong("userId"));
        clientPushRecord.setType(rs.getInt("type"));
        clientPushRecord.setTimes(rs.getInt("times"));
        clientPushRecord.setUpdateTime(rs.getLong("updateTime"));

        return clientPushRecord;
    }

    public Map<Integer, ClientPushContent> getClientPushContentMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ClientPushContent> clientPushContentMap = new HashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall("select * from t_s_client_push_content");
            int i = 1;
            rs = call.executeQuery();
            while (rs.next()) {
                ClientPushContent clientPushContent = new ClientPushContent();
                clientPushContent.setPushType(rs.getInt("pushType"));
                clientPushContent.setMsg(rs.getString("msg"));
                clientPushContent.setTitle(rs.getString("title"));
                clientPushContentMap.put(clientPushContent.getPushType(), clientPushContent);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return clientPushContentMap;
    }
}
